// ignore_for_file: prefer_final_fields
// ignore_for_file: file_names, prefer_const_literals_to_create_immutables, prefer_const_constructors, camel_case_types, avoid_print



import 'package:flutter/cupertino.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:guo_xue_app/util/page_style.dart';
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pickers/time_picker/model/date_mode.dart';
import 'package:flutter_pickers/time_picker/model/suffix.dart';
import 'package:lunar/calendar/Lunar.dart';
import 'package:lunar/calendar/Solar.dart';
import 'package:intl/intl.dart';
import 'package:lunar/calendar/util/LunarUtil.dart';

class BaziXuanzePageLogic extends GetxController {
  /// 性别选择-男
  final male = "男";

  /// 性别选择-女
  final female = "女";

  /// 公历
  final gregorian = "公历";

  /// 农历
  final lunar = "农历";

  ///四柱
  final fourPillars = "四柱";

  String? selectedDate;
  DateTime? dateTime;
  var timesa = "请选择出生时间".obs;
  var timeAll = "".obs;

  ///控制左边选择
  bool leftPressed = true;

  // 所在区域  省 市 区
  String initProvince = '', initCity = '', initTown = '';
  String address = "请选择出生区县(默认北京/选填)";
  List<String> cityCode = <String>["", "", ""];

  int ids = 1;
  int leftIds = 1;
  TextEditingController baziController = TextEditingController();

  ///三项选择
  Widget getButton(String text, bool rightPressed, int id,BuildContext context) {
    _buttonColor(int color) {
      if (color == ids) {
        return PageStyle.paColor.E4C2CFF;
      } else {
        return PageStyle.paColor.whiteFFF;
      }
    }

    return GestureDetector(
      child: Container(
        padding: EdgeInsets.fromLTRB(15.w, 5.h, 15.w, 5.h),
        decoration: BoxDecoration(
          border:
              Border.all(color: PageStyle.paColor.E4C2CFF, width: 1), // 边色与边宽度
          color: _buttonColor(id), // 底色
        ),
        child: Text(text),
      ),
      onTap: () {
        Pickers.showDatePicker(
          context,
          mode: DateMode.YMDHMS,
          // 后缀 默认Suffix.normal()，为空的话Suffix()
          suffix: Suffix(
              years: ' 年',
              month: ' 月',
              days: ' 日',
              hours: ' 点',
              minutes: ' 分钟',
              seconds: ' 秒'),
          onConfirm: (p) {
            print('longer >>> 返回数据：$p');

            selectedDate = formatDate(
                DateTime(p.year!, p.month!, p.day!, p.hour!, p.minute!, p.second!),
                [yyyy, "-", mm, "-", dd, " ", hh, ":", mm, ":", ss]);

            debugPrint("获得 selectedDate 结果为" + selectedDate!);

            dateTime =
                DateTime(p.year!, p.month!, p.day!, p.hour!, p.minute!, p.second!);

            debugPrint("获得 dateTime 结果为" + dateTime.toString());

            // 实例化
            Solar solar = Solar.fromDate(dateTime!);

            debugPrint("获得 solar 结果为" + solar.toYmdHms().toString());

            ///24小时转换12时辰
            List<String> _hourWords = [
              '子',
              '丑',
              '寅',
              '卯',
              '辰',
              '巳',
              '午',
              '未',
              '申',
              '酉',
              '戌',
              '亥'
            ];
            String hour = DateFormat('H').format(dateTime!);
            int index = (int.parse(hour) ~/ 2) % 12;
            String hourWord = _hourWords[index];
            debugPrint("获得 hourWord 结果为" + hourWord);

            String times = DateFormat('h:mm a').format(dateTime!) + ' $hourWord时';
            debugPrint("获得 times 结果为" + times);

            // 转阴历
            Lunar lunar2 = solar.getLunar();
            debugPrint("获得 lunar2 结果为" + lunar2.toString() + hourWord + "时");
            debugPrint("获得 lunar2.toFullString 结果为" + lunar2.toFullString());

            if (ids == 1) {
              timeAll.value = solar.toYmdHms();
              debugPrint("获得 timeAll 结果为" + solar.toYmdHms().toString());
              update();
            } else if (ids == 2) {
              timeAll.value = (lunar2.toString() + " " + hourWord + "时") ;
              debugPrint("获得 timeAll 结果为" + lunar2.toString() + hourWord + "时");
              update();
            }
          },
        );

        switch (id) {
          case 1:
            ids = id;
            update();
            break;
          case 2:
            ids = id;
            update();
            break;
          case 3:
            ids = id;
            update();
            break;
        }
        debugPrint(text);
      },
    );
  }

  /// 男女选择
  Widget leftButton(String text, bool rightPressed, int id) {
    _leftbuttonColor(int color) {
      if (color == leftIds) {
        return PageStyle.paColor.E4C2CFF;
      } else {
        return PageStyle.paColor.whiteFFF;
      }
    }

    return GestureDetector(
      child: Container(
        padding: EdgeInsets.fromLTRB(15.w, 5.h, 15.w, 5.h),
        decoration: BoxDecoration(
          border:
              Border.all(color: PageStyle.paColor.E4C2CFF, width: 1), // 边色与边宽度
          color: _leftbuttonColor(id), // 底色
          //  圆角度
          // borderRadius: _leftborderRadius(id),
        ),
        child: Text(text),
      ),
      onTap: () {
        switch (id) {
          case 1:
            leftIds = id;
            update();
            break;
          case 2:
            leftIds = id;
            update();
            break;
        }
        debugPrint(text);
      },
    );
  }

  ///点击选择省市区
  void Pickes(BuildContext context,) {
    Pickers.showAddressPicker(
      context,
      initProvince: initProvince,
      initCity: initCity,
      initTown: initTown,
      addAllItem: false,
      onConfirm: (p, c, t) {
        initProvince = p;
        initCity = c;
        initTown = t ?? '';
        update();
        print('longer >>> 返回数据：$p');
        print('longer >>> 返回数据：$c');
        print('longer >>> 返回数据：$t');

        // cityCode = Address.getCityCodeByName(
        //     townName: t, cityName: c, provinceName: p);
        // print(cityCode);
        // debugPrint(initProvince);
        // debugPrint(initCity);
        // debugPrint(initTown);
      },
    );
  }

  ///判断省市区文字
  String ProvinceCityTown() {
    if (initProvince == '') {
      return address;
    } else {
      return "${initProvince}-${initCity}-${initTown}";
    }
  }

}
